
package com.e885.mail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

/**
 * @author Grady.Kao
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2000-2008
 */

public class MailReceiver
{
	private String mailServer;

	private String userName;

	private String password;

	public MailReceiver( String mailServer )
	{
		this( mailServer, null, null );
	}

	public MailReceiver( String mailServer, String userName, String password )
	{
		this.mailServer = mailServer;
		this.userName = userName;
		this.password = password;
	}

	public Receiver[] receive() throws MessagingException, IOException
	{
		Properties properties = System.getProperties();

		Session session = Session.getDefaultInstance( properties );

		Store store = session.getStore( "pop3" );

		store.connect( mailServer, userName, password );

		Folder folder = store.getDefaultFolder();

		if( folder == null )
			return new Receiver[ 0 ];
		else
		{
			folder = folder.getFolder( "INBOX" );

			if( folder == null )
				return new Receiver[ 0 ];
			else
			{
				folder.open( Folder.READ_WRITE );

				Message[] messages = folder.getMessages();

				int length = messages.length;

				Receiver[] receivers = new Receiver[ length ];

				for( int i = 0; i < length; i++ )
				{
					receivers[ i ] = new Receiver();

					InternetAddress[] internetAddress = ( InternetAddress[] )messages[ i ].getFrom();

					Part part = ( Part )messages[ i ];

					Object content = part.getContent();

					if( content instanceof Multipart )
						part = ( ( Multipart )content ).getBodyPart( 0 );

					String contentType = part.getContentType();

					StringBuffer contentBuffer = new StringBuffer();
					if( contentType.startsWith( "text/plain" ) || contentType.startsWith( "text/html" ) )
					{
						BufferedReader reader = new BufferedReader( new InputStreamReader( part.getInputStream() ) );

						String thisLine = reader.readLine();
						while( thisLine != null )
						{
							contentBuffer.append( thisLine );
							contentBuffer.append( "\n" );

							thisLine = reader.readLine();
						}
					}

					receivers[ i ].setFromAddress( internetAddress[ 0 ].getAddress() );
					receivers[ i ].setFromPerson( internetAddress[ 0 ].getPersonal() );
					receivers[ i ].setSubject( messages[ i ].getSubject() );
					receivers[ i ].setContentType( contentType );
					receivers[ i ].setContent( contentBuffer.toString() );

					messages[ i ].setFlag( Flags.Flag.DELETED, true );
				}

				folder.close( true );

				store.close();

				return receivers;
			}
		}
	}

	/**
	 * @return the userName
	 */
	public String getUserName()
	{
		return userName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName( String userName )
	{
		this.userName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword()
	{
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword( String password )
	{
		this.password = password;
	}

	/**
	 * @return the mailServer
	 */
	public String getMailServer()
	{
		return mailServer;
	}

}
